package com.cdxy.biz.process.impl;

import com.cdxy.biz.process.ILoginBizProcess;
import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.BizErrorEnum;
import com.cdxy.enums.KaptchaEnum;
import com.cdxy.enums.redis.RedisCacheGroupEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.base.exception.enums.CommonErrorEnum;
import com.cdxy.model.dto.req.*;
import com.cdxy.model.dto.resp.ImgCodeResp;
import com.cdxy.model.dto.resp.UserInfoResp;
import com.cdxy.service.*;
import com.cdxy.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: tangy
 * @Date: 2020/2/23 16:55
 */
@Service
public class LoginBizProcessImpl implements ILoginBizProcess {

    private ISmsService smsService;

    private IEmailService emailService;

    private IImgService imgService;

    private ILoginService loginService;

    private IBindService bindService;

    @Autowired
    private AxUserRepository axUserRepository;

    public LoginBizProcessImpl(ISmsService smsService, IEmailService emailService, IImgService imgService,
                               ILoginService loginService, IBindService bindService) {
        this.smsService = smsService;
        this.emailService = emailService;
        this.imgService = imgService;
        this.loginService = loginService;
        this.bindService = bindService;
    }


    @Override
    public void sendSms(SendMobileCodeReq sendMobileCodeReq) {
        String phone = sendMobileCodeReq.getPhone();
        // 校验数据
        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }

        // 组装数据
        KaptchaEnum.BusinessCode businessCode = KaptchaEnum.BusinessCode.LOGIN_SMS;
        smsService.sendSms(businessCode, phone);
    }

    @Override
    public void sendEmail(SendEmailCodeReq sendEmailCodeReq) {
        String email = sendEmailCodeReq.getEmail();
        if (!CommonUtil.checkEmail(email)) {
            throw new BusinessException(CommonErrorEnum.PARAM_ERROR, "邮箱错误");
        }
        AxUserDO axUserDO = axUserRepository.getByEmail(email);
        if (axUserDO == null) {
            throw new BusinessException(BizErrorEnum.LOGIN_ACCOUNT_NOT_EXIST);
        }

        KaptchaEnum.BusinessCode businessCode = KaptchaEnum.BusinessCode.LOGIN_EMAIL;
        emailService.sendEmail(businessCode, email);
    }

    @Override
    public ImgCodeResp getImgCode() {
        RedisCacheGroupEnum redisCacheGroupEnum = RedisCacheGroupEnum.LOGIN_IMG_CODE;
        return imgService.getImgCode(redisCacheGroupEnum);
    }

    @Override
    public UserInfoResp mobileCodeLogin(LoginMobileCodeReq loginMobileCodeReq) {
        return loginService.mobileCodeLogin(loginMobileCodeReq);
    }

    @Override
    public UserInfoResp emailCodeLogin(LoginEmailCodeReq loginEmailCodeReq) {
        return loginService.emailCodeLogin(loginEmailCodeReq);
    }

    @Override
    public UserInfoResp accountLogin(LoginAccountReq loginAccountReq) {
        return loginService.accountLogin(loginAccountReq);
    }

    @Override
    public void sendSmsBindEmail(SendMobileCodeReq sendMobileCodeReq) {
        String phone = sendMobileCodeReq.getPhone();
        // 组装数据
        KaptchaEnum.BusinessCode businessCode = KaptchaEnum.BusinessCode.LOGIN_BIND_EMAIL;
        smsService.sendSms(businessCode, phone);
    }

    @Override
    public UserInfoResp emailLoginBindMobile(LoginBindEmailReq loginBindEmailReq) {
        return bindService.loginBindEmail(loginBindEmailReq);
    }

    @Override
    public void loginOut(UserWithLoginReq userWithLoginReq) {
        loginService.loginOut(userWithLoginReq);
    }
}
